package com.movie.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.movie.bean.MovieNew;
import com.movie.bean.MovieNewList;
import com.movie.bean.MoviePinLun;
import com.movie.bean.MoviePinLunList;
import com.movie.cache.AsyncImageLoader;
import com.movie.task.MainActTask;
import com.movie.util.Log;
import com.movie.util.RoateUtil;

/**
 * 03-影片介绍
 * 
 * @author Administrator
 * 
 */
public class MovieDesAct extends BaseActivity implements
		MainActTask.UpdateListView {
	private LinearLayout pic_listview = null;
	private ImageView imageUrl = null;
	private RatingBar pointtrate = null;
	private TextView name = null;
	private TextView time = null;
//	private TextView sharecount = null;
//	private TextView goodcount = null;
	private TextView point = null;
	private TextView ping = null;
	private LinearLayout feng_l = null;

	private MoviePinLunList mMoviePinLunList = null;
	private Context mContext = MovieDesAct.this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.moviedesact);
		initBackTitleBar();

		onClick();

		mMovieNewList = new MovieNewList();
		mMainActTask = new MainActTask(this, mMovieNewList);
		mMainActTask.execute();

		pic_listview = (LinearLayout) findViewById(R.id.pic_listview);
		mMoviePinLunList = new MoviePinLunList();
		MoviePinLun mMoviePinLun = null;
		for (int i = 0; i < 10; i++) {
			mMoviePinLun = new MoviePinLun();
			mMoviePinLun.name = "Rainbow" + i;
			mMoviePinLun.imageUrl = "图片" + i;
			mMoviePinLun.time = i + "分钟前";
			mMoviePinLun.sharecount = i + "5";
			mMoviePinLun.goodcount = i + "5";
			if (i % 2 == 0) {
				mMoviePinLun.point = "5";
			} else {
				mMoviePinLun.point = "";
			}

			mMoviePinLun.ping = "真的不错哦真的不错哦真的不错哦  ";
			mMoviePinLunList.addMoviePinLun(mMoviePinLun);
		}
		showMoviePinLunList(mMoviePinLunList);

	}

	private ImageButton play = null;
	private ImageView shoucang = null;
	private ImageView write = null;
	private ImageView share = null;

	private int[] backgrounds=new int[]{R.drawable.shoucang_selector,
            R.drawable.yishoucang
           };
	private boolean hasSave=false;
	private void onClick() {
		play = (ImageButton) findViewById(R.id.play);
		shoucang = (ImageView) findViewById(R.id.shoucang);
		write = (ImageView) findViewById(R.id.write);
		share = (ImageView) findViewById(R.id.share);
		
		if(hasSave)
		{
			shoucang.setBackgroundResource(backgrounds[1]);
		}else
		{
			shoucang.setBackgroundResource(backgrounds[0]);
		}
		play.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//剧照
				Intent intent = new Intent(mContext, MoviePicsAct.class);
							mContext.startActivity(intent);
			}
		});

		shoucang.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.i("---shoucang-----onClick-----hasSave"+hasSave);
				if(hasSave)
				{
					shoucang.setBackgroundResource(backgrounds[0]);
					hasSave=false;
				}else
				{
					shoucang.setBackgroundResource(backgrounds[1]);
					hasSave=true;
				}
			}
		});

		write.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
         //写评论
				Intent intent = new Intent(mContext, WritePinLunAct.class);
							mContext.startActivity(intent);
			}
		});
		
		share.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
			}
		});
	}

	class MoviePinLunItemClick implements OnClickListener {

		private boolean canrun = true;

		public void setCanRun(boolean flag) {
			this.canrun = flag;
		}

		public MoviePinLunItemClick(MoviePinLun item) {
			this.item = item;
		}

		private MoviePinLun item;

		public void onClick(View v) {

		}

	}

	private void showMoviePinLunList(MoviePinLunList mMoviePinLunList) {
		LayoutInflater layoutFlater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (pic_listview != null) {
			pic_listview.removeAllViews();
			int lines = mMoviePinLunList.mMoviePinLuns.size();
			LinearLayout convertView = null;
			MoviePinLun bean = null;
			MoviePinLunItemClick listen = null;
			for (int i = 0; i < lines; i++) {
				// 布局
				convertView = (LinearLayout) layoutFlater.inflate(
						R.layout.movie_pinlun_list_item, null);
				// 分割线
				// line = (ImageView) layoutFlater.inflate(R.layout.line, null);

				// 数据
				bean = mMoviePinLunList.mMoviePinLuns.get(i);

				imageUrl = (ImageView) convertView.findViewById(R.id.imageUrl);
				pointtrate = (RatingBar) convertView
						.findViewById(R.id.pointtrate);
				name = (TextView) convertView.findViewById(R.id.name);
				time = (TextView) convertView.findViewById(R.id.time);
//				sharecount = (TextView) convertView
//						.findViewById(R.id.sharecount);
//				goodcount = (TextView) convertView.findViewById(R.id.goodcount);
				point = (TextView) convertView.findViewById(R.id.point);
				ping = (TextView) convertView.findViewById(R.id.ping);
				feng_l = (LinearLayout) convertView.findViewById(R.id.feng_l);

				// imageUrl.setImageResource(R.drawable.icon);
				name.setText(bean.name);
				time.setText(bean.time);
//				sharecount.setText(bean.sharecount);
//				goodcount.setText(bean.goodcount);
				ping.setText(bean.ping);
				//
				if ("".equals(bean.point)) {
					feng_l.setVisibility(View.GONE);
				} else {
					feng_l.setVisibility(View.VISIBLE);
					point.setText(bean.point);
				}
				listen = new MoviePinLunItemClick(bean);
				// 事件
				convertView.setOnClickListener(listen);
				pic_listview.addView(convertView);
			}
		}

	}

	// 释放
	public void release() {
		if (pic_listview != null) {
			pic_listview.removeAllViews();
			mMoviePinLunList.release();
			pic_listview = null;
		}
		imageUrl = null;
		name = null;
		time = null;
//		sharecount = null;
//		goodcount = null;
		point = null;
		ping = null;
		if (feng_l != null) {
			feng_l.removeAllViews();
			feng_l = null;
		}
	}

	// -------------------影片列表-----------START
	public void initListView() {
		more_listview = (ListView) findViewById(R.id.more_listview);
		mMovieNewListAdapter = new MovieNewListAdapter();
		more_listview.setAdapter(mMovieNewListAdapter);

		listview_menu = (ImageView) findViewById(R.id.listview_menu);
		listview_menu.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (ismListViewHidden) {
					more_listview.setVisibility(View.VISIBLE);
					ismListViewHidden = false;
				} else {
					more_listview.setVisibility(View.GONE);
					ismListViewHidden = true;
				}

			}
		});

	}

	private int selected = -1;
	private ImageView listview_menu = null;
	private boolean ismListViewHidden = true;
	private ListView more_listview = null;
	private MovieNewListAdapter mMovieNewListAdapter = null;
	private MovieNewList mMovieNewList = null;
	private MainActTask mMainActTask = null;
	public static AsyncImageLoader mAsyncImageLoader = null;

	public class MovieNewListAdapter extends BaseAdapter {

		private LayoutInflater layoutFlater;

		public MovieNewListAdapter() {
			super();
			layoutFlater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return mMovieNewList.mMovieNews.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolderListViewItem holder;
			if (convertView == null) {
				convertView = layoutFlater.inflate(
						R.layout.movie_new_list_item, null);
				holder = new ViewHolderListViewItem();
				holder.imageUrl = (ImageView) convertView
						.findViewById(R.id.imageUrl);
				holder.point = (TextView) convertView.findViewById(R.id.point);
				holder.pointbg = (TextView) convertView
						.findViewById(R.id.pointbg);
				holder.date = (TextView) convertView.findViewById(R.id.date);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.date_l = (LinearLayout) convertView
						.findViewById(R.id.date_l);
				RoateUtil.tranlateCenter(mContext, holder.point);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolderListViewItem) convertView.getTag();
				holder.point.setVisibility(View.INVISIBLE);
				holder.pointbg.setVisibility(View.INVISIBLE);
				holder.date_l.setVisibility(View.INVISIBLE);
			}
			MovieNew item = mMovieNewList.mMovieNews.get(position);
			String url = item.imageUrl;

			// ImageView cacheImage = holder.imageUrl;
			// holder.imageUrl.setTag(item.imageUrl);
			// MainAct.mAsyncImageLoader.getListViewLogoItem(mContext, url,
			// cacheImage, more_listview);

			holder.point.setText(item.point);
			holder.date.setText(item.date);

			if ("0".equals(item.status))// 已上映
			{
				holder.date_l.setVisibility(View.INVISIBLE);

				holder.point.setVisibility(View.VISIBLE);
				holder.pointbg.setVisibility(View.VISIBLE);
			}

			if ("1".equals(item.status))// 未上映
			{
				holder.date_l.setVisibility(View.VISIBLE);
				holder.point.setVisibility(View.INVISIBLE);
				holder.pointbg.setVisibility(View.INVISIBLE);
			}
			holder.name.setText(item.name);
			if (selected != -1) {
				if (selected == position) {
					holder.name.setTextColor(0xB35201);
				} else {
					holder.name.setTextColor(0xB1AFAD);
				}
			}

			return convertView;
		}
	}

	private static class ViewHolderListViewItem {

		public ImageView imageUrl;
		public TextView point;
		public TextView pointbg;
		public TextView date;
		public TextView name;
		public LinearLayout date_l;
	}

	// -------------------影片列表----------END
	@Override
	public void showprogressBar() {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateListView() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onListViewListeners() {
		// more_listview.setOnItemSelectedListener(new OnItemSelectedListener()
		// {
		//
		// public void onItemSelected(AdapterView<?> mAdapterView,
		// View parent, int position, long id) {
		// showMovieItem(mMovieNewList.mMovieNews.get(position));
		// }
		//
		// public void onNothingSelected(AdapterView<?> arg0) {
		// }
		// });
		more_listview.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> mAdapterView, View parent,
					int position, long id) {
				// 跳到影片界绍

				Intent intent = new Intent(mContext, MovieDesAct.class);
				mContext.startActivity(intent);
			}

		});

	}
}
